<template><div><h2 id="部署" tabindex="-1"><a class="header-anchor" href="#部署"><span>部署</span></a></h2>
<p>Laravel 5.5 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的部署" tabindex="-1"><a class="header-anchor" href="#laravel-的部署"><span>Laravel 的部署</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#server-configuration">服务器配置</a>
<ul>
<li><a href="#nginx">Nginx</a></li>
</ul>
</li>
<li><a href="#optimization">优化</a>
<ul>
<li><a href="#autoloader-optimization">优化自动加载</a></li>
<li><a href="#optimizing-configuration-loading">优化配置加载</a></li>
<li><a href="#optimizing-route-loading">优化路由加载</a></li>
</ul>
</li>
<li><a href="#deploying-with-forge">Forge 部署</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>当你准备好将 Laravel 应用部署到生产环境时，你可以执行一些操作来确保应用程序尽可能高效地运行。在本文档中介绍一些能确保 Laravel 应用被正确部署。</p>
<h2 id="服务器配置" tabindex="-1"><a class="header-anchor" href="#服务器配置"><span>服务器配置</span></a></h2>
<h3 id="nginx" tabindex="-1"><a class="header-anchor" href="#nginx"><span>Nginx</span></a></h3>
<p>如果你将应用程序部署到运行 Nginx 的服务器，可以使用下面的内容来配置 Web 服务器。这个文件可能需要根据你的服务器配置进行自定义。你可以考虑使用 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> 等服务协助管理你的服务器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">server <span class="token punctuation">{</span></span>
<span class="line">    listen <span class="token number">80</span><span class="token punctuation">;</span></span>
<span class="line">    server_name example<span class="token operator">.</span>com<span class="token punctuation">;</span></span>
<span class="line">    root <span class="token operator">/</span>example<span class="token operator">.</span>com<span class="token operator">/</span><span class="token keyword">public</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    add_header <span class="token constant">X</span><span class="token operator">-</span>Frame<span class="token operator">-</span>Options <span class="token string double-quoted-string">"SAMEORIGIN"</span><span class="token punctuation">;</span></span>
<span class="line">    add_header <span class="token constant">X</span><span class="token operator">-</span><span class="token constant">XSS</span><span class="token operator">-</span>Protection <span class="token string double-quoted-string">"1; mode=block"</span><span class="token punctuation">;</span></span>
<span class="line">    add_header <span class="token constant">X</span><span class="token operator">-</span>Content<span class="token operator">-</span>Type<span class="token operator">-</span>Options <span class="token string double-quoted-string">"nosniff"</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    index index<span class="token operator">.</span>html index<span class="token operator">.</span>htm index<span class="token operator">.</span>php<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    charset utf<span class="token operator">-</span><span class="token number">8</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">/</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name type-declaration">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span><span class="token operator">/</span> <span class="token operator">/</span>index<span class="token operator">.</span>php<span class="token operator">?</span><span class="token variable">$query_string</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">=</span> <span class="token operator">/</span>favicon<span class="token operator">.</span>ico <span class="token punctuation">{</span> access_log off<span class="token punctuation">;</span> log_not_found off<span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line">    location <span class="token operator">=</span> <span class="token operator">/</span>robots<span class="token operator">.</span>txt  <span class="token punctuation">{</span> access_log off<span class="token punctuation">;</span> log_not_found off<span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    error_page <span class="token number">404</span> <span class="token operator">/</span>index<span class="token operator">.</span>php<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">~</span> \<span class="token operator">.</span><span class="token class-name type-declaration">php</span>$ <span class="token punctuation">{</span></span>
<span class="line">        fastcgi_split_path_info <span class="token operator">^</span><span class="token punctuation">(</span><span class="token operator">.</span><span class="token operator">+</span>\<span class="token operator">.</span>php<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token operator">/</span><span class="token operator">.</span><span class="token operator">+</span><span class="token punctuation">)</span>$<span class="token punctuation">;</span></span>
<span class="line">        fastcgi_pass unix<span class="token punctuation">:</span><span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>run<span class="token operator">/</span>php<span class="token operator">/</span>php7<span class="token operator">.</span><span class="token number">1</span><span class="token operator">-</span>fpm<span class="token operator">.</span>sock<span class="token punctuation">;</span></span>
<span class="line">        fastcgi_index index<span class="token operator">.</span>php<span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">include</span> fastcgi_params<span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    location <span class="token operator">~</span> <span class="token operator">/</span>\<span class="token operator">.</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">!</span>well<span class="token operator">-</span>known<span class="token punctuation">)</span><span class="token operator">.</span><span class="token operator">*</span> <span class="token punctuation">{</span></span>
<span class="line">        deny all<span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="优化" tabindex="-1"><a class="header-anchor" href="#优化"><span>优化</span></a></h2>
<h3 id="优化自动加载" tabindex="-1"><a class="header-anchor" href="#优化自动加载"><span>优化自动加载</span></a></h3>
<p>部署项目到生产环境时，请确保你优化了 Composer 类的自动加载映射，以便 Composer 可以快速找到正确文件为给定类加载：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer install <span class="token operator">--</span>optimize<span class="token operator">-</span>autoloader</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{tip} 除了优化自动加载之外，还应该确保项目的源代码管理库中包含了 <code v-pre>composer.lock</code> 文件。因为当 <code v-pre>composer.lock</code> 文件存在时，项目的依赖项可以被更快地安装。</p>
</blockquote>
<h3 id="优化配置加载" tabindex="-1"><a class="header-anchor" href="#优化配置加载"><span>优化配置加载</span></a></h3>
<p>将应用部署到生产环境时，记得在部署过程中运行 Artisan 命令 <code v-pre>config:cache</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan config<span class="token punctuation">:</span>cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令可以将所有 Laravel 的配置文件合并到单个文件中缓存，此举能大大减少框架在加载配置值时必须执行的系统文件的数量。</p>
<h3 id="优化路由加载" tabindex="-1"><a class="header-anchor" href="#优化路由加载"><span>优化路由加载</span></a></h3>
<p>如果你构建的是具有许多路由的大型应用程序，那你应该在部署过程中运行 Artisan 命令 <code v-pre>route:cache</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan route<span class="token punctuation">:</span>cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令可以将所有路由注册减少为缓存文件中的单个方法调用，以达到当应用程序在注册数百条路由时，提高路由注册的性能。</p>
<blockquote>
<p>{note} 由于此功能使用 PHP 序列化，而 PHP 无法序列化闭包，因此只能缓存应用程序中基于控制器的路由。</p>
</blockquote>
<h2 id="forge-部署" tabindex="-1"><a class="header-anchor" href="#forge-部署"><span>Forge 部署</span></a></h2>
<p>如果你还没有准备好管理自己的服务器配置，或者你的服务器没有配置 Laravel 应用程序所需的各种服务，<a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> 是一个不错的选择。</p>
<p>Laravel Forge 可以在各种基础设施提供商（如 DigitalOcean、Linode、AWS 等）上创建服务器。此外，Forge 还能安装和管理构建 Laravel 应用程序所需的所有工具，如 Nginx、MySQL、Redis、Memcached、Beanstalk 等。</p>
<h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>翻译</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


